package com.jason.sms.client;

import java.net.MalformedURLException;

import javax.xml.namespace.QName;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.commons.codec.digest.DigestUtils;

public class TestKuanle {
	
	
	private String remoteUrl="http://202.105.212.146:8080/jboss-net/services/";
	private String esunUrl="http://121.33.239.252:9980/services/MessageService";
	private String username="test";
	private String pwd="test";
	public String uc="07633380955";
	public String ucPwd="123456";
	private String REGISTER = "Register";
	private String CALLBACK = "CallBack";
	private String SENDSMS="SendSMS";
	private String SENDSMSMETHOD="sendSMS";
	private String CALLBACKMETHOD="EchoOfSendSMS";
	private String GETRANDOM = "GetRandom";
	private String SETCALLBACKADDR = "setCallBackAddr";
	
	/**账号：07633380955  密码：123456账号：kltx40812691 密码：123456 试试这两个，
	 * 通用的webService调用方法s
	 */
	public String invokeValue(String username,String password,String url,String qName,String qValue,Object[] object){
		String str=null;
		Service service = new Service();		
		Call call;
		try {
			call = (Call)service.createCall();
			call.setUseSOAPAction(true);		
			call.setUsername(username);
			call.setPassword(password);		
			call.setTargetEndpointAddress(url);
			call.setOperationName(new QName(qName,qValue));//接口级认证
			str =(String)call.invoke( object);
		} catch (Exception e) {
			str="Exception";
		} 	
		return str;
	}
	
	/**
	 * 获取随机数
	 * @throws MalformedURLException 
	 */
	public String getRandom(){
		Object []object=null;
		String random = invokeValue(username, pwd, (remoteUrl + REGISTER), REGISTER, GETRANDOM,object);		
		return random;
	}
	
	/**
	 * 该接口用于第三方服务器注册它的回调WebService接口的地址，获取连接id
	 * */	
	public String setCallBackAddr(String random){
		Object []object = {uc,DigestUtils.md5Hex(random + ucPwd+ucPwd),random,esunUrl};		
		String connId = invokeValue(username, pwd,(remoteUrl + REGISTER), REGISTER, SETCALLBACKADDR, object);
		return connId;
	}
	
	
	
	public String sendSMS(String random,String connId,String callee[],String content,int msgid){		
		Object []object = {uc,DigestUtils.md5Hex(random + ucPwd+ucPwd),random,callee,"1",content,msgid,connId};
		String res = invokeValue(username, pwd,(remoteUrl + SENDSMS), SENDSMS, SENDSMSMETHOD, object);
		return res;
	}
	
	/**
     * 提供正常字符串的BASE64加密
     */
    public String getEncoderStr(String str_to_base64) {
        String str = (new sun.misc.BASE64Encoder()).encode(str_to_base64.getBytes());
        return str;
    }
	
	public static void main(String[] arg){
		TestKuanle sendM = new TestKuanle();
		//发送用户号码先为空
		String sendUserId="07633380955";
		String receiveUserId = "13660587367";//接收者号码		
		String content = "test";
	
		String connId=sendM.setCallBackAddr(sendM.getRandom());
		System.out.println("MessageAction connId: " + connId);		
		//int msgid=MessageServlet.handMsgid;
		int msgid = 1;
		String callee[]={receiveUserId};
		String res = sendM.sendSMS(sendM.getRandom(), connId, callee, sendM.getEncoderStr(content), msgid);//短信息加执结果
		System.out.println(res + " 短信息加执结果 ");
	}

}
